import request from '@/utils/request'
import { RoleResponseData, Role, MenuResponseData } from './type'

enum ROLE_ACL_API_URL {
  /**
   * 获取角色（不是用户）数据的接口地址
   */
  GET_ALL_ROLE_URL = '/admin/acl/role',

  /**
   * 新增岗位的接口地址
   */
  ADD_ROLE_URL = '/admin/acl/role/save',

  /**
   * 更新已有的职位的接口地址
   */
  UPDATE_ROLE_URL = '/admin/acl/role/update',

  /**
   * 获取当前职位的所有可赋予权限以及已有权限数据的接口地址
   */
  GET_ALL_PERMISSION_URL = '/admin/acl/permission/toAssign',

  /**
   * 分配权限的接口地址
   */
  SET_PERMISSION_URL = '/admin/acl/permission/doAssign',

  /**
   * 删除职位
   */
  DELETE_ROLE_URL = '/admin/acl/role/remove',
}

/**
 * 获取角色数据的接口
 * @param pageNo
 * @param limit
 * @param roleName
 * @returns Promise<RoleResponseData>
 */
export const reqAllRoleList = (
  pageNo: number,
  limit: number,
  roleName: string,
): Promise<RoleResponseData> =>
  request.get<any, RoleResponseData>(
    ROLE_ACL_API_URL.GET_ALL_ROLE_URL + `/${pageNo}/${limit}/?roleName=${roleName}`,
  )

/**
 * 添加/更新岗位的接口
 * @param data
 * @returns Promise<any>
 */
export const reqAddOrUpdateRole = (data: Role): Promise<any> => {
  if (data.id) {
    return request.put<any, any>(ROLE_ACL_API_URL.UPDATE_ROLE_URL, data)
  } else {
    return request.post<any, any>(ROLE_ACL_API_URL.ADD_ROLE_URL, data)
  }
}

/**
 * 获取当前值为的所有可赋予权限以及当前职位已有权限数据的接口
 * @param roleId
 * @returns Promise<MenuResponseData>
 */
export const reqAllPermission = (roleId: number): Promise<MenuResponseData> =>
  request.get<any, MenuResponseData>(ROLE_ACL_API_URL.GET_ALL_PERMISSION_URL + `/${roleId}`)

/**
 * 分配权限的接口
 * @param roleId
 * @param permissionId
 * @returns Promise<any>
 */
export const reqSetPermission = (roleId: number, permissionId: number[]): Promise<any> =>
  request.post<any, any>(
    ROLE_ACL_API_URL.SET_PERMISSION_URL + `?roleId=${roleId}&permissionId=${permissionId}`,
  )

/**
 * 删除职位的接口
 * @param Id
 * @returns Promise<any>
 */
export const reqDeleteRole = (Id: number): Promise<any> =>
  request.delete<any, any>(ROLE_ACL_API_URL.DELETE_ROLE_URL + `/${Id}`)
